package com.base.taskreports.task.job;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;

import com.base.taskreports.common.utlis.DateUtil;
import com.base.taskreports.task.entity.TimeTaskEntity;
import com.base.taskreports.task.entity.UserMasterEntity;
import com.base.taskreports.task.service.TimedTaskService;
import com.base.taskreports.task.service.UserMasterService;
import com.base.taskreports.task.service.UserSalveService;
import com.vip.saturn.job.AbstractSaturnJavaJob;
import com.vip.saturn.job.SaturnJobExecutionContext;
import com.vip.saturn.job.SaturnJobReturn;

public class DemoJob extends AbstractSaturnJavaJob {

	@Autowired
	private UserMasterService userMasterService;
	@Autowired
	private UserSalveService userSalveService;
	
	@Autowired
	private TimedTaskService timedTaskService;
	
	@Override
	public SaturnJobReturn handleJavaJob(String jobName, Integer shardItem, String shardParam,
			SaturnJobExecutionContext shardingContext) throws InterruptedException {
		
		TimeTaskEntity timedTask = new TimeTaskEntity();
		timedTask.setTaskId(1);
        timedTask.setCron(DateUtil.timeToCron("2021-12-01 00:00:00"));
        timedTask.setName("李四");
        timedTask.setJobGroup("remind");
        timedTask.setCreateTime(new Date());
        timedTask.setDeleteFlag(0);
        timedTaskService.save(timedTask);
        
        
        
		String table = shardingContext.getJobName();
		String jobParameter = shardingContext.getJobParameter();
		List<UserMasterEntity> getuser = userMasterService.getuser(table);
		System.out.println("获取主表数据:"+table+"的"+getuser.size());
		
		userSalveService.SaveUserMasterList(getuser,table);
		System.out.println("插入从表数据:"+table+"的111");
		
		
		
		//结束时间
		timedTask.setUpdateTime(new Date());
		timedTaskService.updateById(timedTask);
		// TODO Auto-generated method stub
		return new SaturnJobReturn("I am the return result of shard item " + shardItem);
	}


	/**
	 * @param args
	 * @throws Exception
	public static void main(String[] args) throws Exception {
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181")
				.namespace("mydomain").sessionTimeoutMs(10000).retryPolicy(retryPolicy).build();
		client.start();
		addJavaJob(client, "demoJavaJob");
		System.out.println("done add a java-job.");
	}

	protected static void addJavaJob(CuratorFramework client, String jobName) throws Exception {
		String jobConfigNode = "/$Jobs/" + jobName + "/config/";
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "jobType", "JAVA_JOB".getBytes()); */
		//client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "cron", "*/5 * * * * ?".getBytes());
		/**client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "shardingTotalCount", "5".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "shardingItemParameters","0=0,1=1,2=2,3=3,4=4".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "enabled", "false".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "failover", "true".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "timeoutSeconds", "0".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "maxTimeDiffSeconds", "-1".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "processCountIntervalSeconds","5".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "showNormalLog", "true".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "loadLevel", "1".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "monitorExecution", "true".getBytes());
		client.create().creatingParentsIfNeeded().forPath(jobConfigNode + "jobClass","demo.DemoJavaJob".getBytes());
		Thread.sleep(3000);
		client.setData().forPath(jobConfigNode + "enabled", "true".getBytes());
	}*/

	
	
}
